<template>
    <el-card class="box_card" shadow="never">
        <div class="title-box">
            <p>客户趋势图</p>
            <el-button @click="exportData">导出</el-button>
        </div>
        <div v-if="line_data.dateList.length" id="echarts" ref="myChartLineRef"></div>
        <div v-else class="none_data">
            <img class="no_img" src="@/assets/noData.png" alt="" />
            <div>暂无数据</div>
        </div>
    </el-card>
</template>

<script lang="ts" setup>
import { ref, inject, watch, nextTick } from 'vue';

const props = defineProps(['lineData']);
const emit = defineEmits(['exportData']);

const myChartLineRef = ref<any>(null);
const echarts: any = inject('ec');

const line_data = ref({
    nameList: [],
    dateList: [],
    dataList: [],
    last_time: ''
});
// 折线图配置项
const getEchartsLine = () => {
    const myChartLine = echarts?.init(myChartLineRef.value);
    const option = {
        color: [
            '#FF9228',
            '#2EB5E3',
            '#4AD983',
            '#FF724D',
            '#ED1F45',
            '#FFB323',
            '#4A7EE6',
            '#8C5FBC',
            '#30CBA8',
            '#FDD300',
            '#CE31CE',
            '#5AE2E0',
            '#668EFF'
        ],
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            x: 'center',
            y: 'bottom',
            bottom: 30,
            padding: [0, 0, 10, 0],
            data: line_data.value.nameList
        },
        grid: {
            top: 20,
            bottom: 60,
            left: 32,
            right: 40,
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: line_data.value.dateList,
            boundaryGap: false,
            offset: 3
        },
        yAxis: {
            type: 'value',
            name: '',
            nameGap: 10, // y轴name与横纵坐标轴线的间距
            nameTextStyle: {
                padding: [0, 35, 10, 0] // 加上padding可以调整其位置
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                color: '#969696'
            },
            splitLine: {
                lineStyle: {
                    type: 'line',
                    color: '#e8e8e8' // 分割线颜色
                }
            }
        },
        series: line_data.value.dataList
    };
    myChartLine.setOption(option);
    window.addEventListener('resize', function () {
        if (myChartLine) {
            myChartLine.resize();
        }
    });
};
watch(
    () => props.lineData,
    val => {
        line_data.value = val || [];
        line_data.value.dataList = val.nameList.map((item: any, index: any) => {
            return {
                name: item,
                type: 'line',
                data: val.dataList[index],
                smooth: true,
                symbol: 'circle'
            };
        });
        nextTick(() => {
            getEchartsLine();
        });
    }
);
const exportData = e => {
    emit('exportData', e);
};
</script>

<style lang="scss" scoped>
.box_card {
    margin-top: 15px;
    width: 100%;

    // padding: 0;
    .title-box {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 11px 24px;
        border-bottom: 1px solid #eaeaea;

        p {
            font-size: 16px;
            font-weight: 500;
            color: #33302d;
            display: flex;
            align-items: center;
        }
    }

    #echarts {
        display: flex;
        justify-content: center;
        height: 400px;
        padding: 24px;
    }

    .none_data {
        margin: 100px 0;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        color: #909399;

        img {
            transform: translateX(15%);
        }
    }
}

:deep(.el-card__body) {
    padding: 0;
}
</style>
